﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.AdvTree;
using DevComponents.DotNetBar;
using System.Diagnostics;
using DevComponents.DotNetBar.Controls;


namespace project_assess.assess_relate
{
    public partial class para_frm : Office2007Form
    {
        Mfrm mfrm;
        DataBase m_db;
        public para_frm(Mfrm frm )
        {
            InitializeComponent();
            m_db = frm.m_db;
            mfrm = frm;
            load_para();

        }
        #region 加载参数
        void load_para()
        {
            string name = mfrm.curprjid + "_para";
            if (m_db.IsTableInDataBase(name))
            {
                this.Text = "修改参数---独立参数";
            }
            else
            {
                this.Text = "修改参数---公共参数";
                btn_apply.Enabled = false;
            }
            int count = this.Controls.Count;
            for (int i = 0; i < count; i++)
            {
                Control c = Controls[i];
                if (c.GetType().Name == "TextBoxX")
                {
                    TextBoxX textbox = c as TextBoxX;
                    double val = mfrm.get_para(textbox.Name);
                    textbox.Text = val.ToString("p");
                }
                else
                {
                    load_para(c);
                }
            }
        }
        void load_para(Control cc)
        {
            int count = cc.Controls.Count;
            if (count == 0)
            {
                return;
            }
            for (int i = 0; i < count; i++)
            {
                Control c = cc.Controls[i];
                if (c.GetType().Name == "TextBoxX")
                {
                    TextBoxX textbox = c as TextBoxX;
                    double val = mfrm.get_para(textbox.Name);
                    textbox.Text = val.ToString("p");
                }
                else
                {
                    load_para(c);
                }
            }
        }
        #endregion
        private void buttonX1_Click(object sender, EventArgs e)
        {
            Process cal = Process.Start("calc.exe");
        }
        #region 保存参数
        private void btn_apply_Click(object sender, EventArgs e)
        {
            message_frm frm = new message_frm("是否应用参数修改", "确认", icon_img.query);
            frm.ShowDialog(this);
            if (frm.yesorno == false)
            {
                frm.Close();
                return;
            }
            apply_change();
        }
        void apply_change()
        {
            int count = this.Controls.Count;
            for (int i = 0; i < count; i++)
            {
                Control c = Controls[i];
                if (c.GetType().Name == "TextBoxX")
                {
                    TextBoxX textbox = c as TextBoxX;
                    double val = Convert.ToDouble(textbox.Text.Replace("%", "")) / 100;
                    string sql = "update [parameter] set [参数值]=" 
                        + val + " where [参数名]='" + textbox.Name + "'";
                    m_db.ExecuteSQL(sql);
                }
                else
                {
                    apply_change(c);
                }
            }
            mfrm.msg("已完成参数修改");
        }
        void apply_change(Control cc)
        {
            int count = cc.Controls.Count;
            if (count == 0)
            {
                return;
            }
            for (int i = 0; i < count; i++)
            {
                Control c = cc.Controls[i];
                if (c.GetType().Name == "TextBoxX")
                {
                    TextBoxX textbox = c as TextBoxX;
                    double val = Convert.ToDouble(textbox.Text.Replace("%", "")) / 100;
                    string sql = "update [" + mfrm.get_para_database() + "] set [参数值]="
                + val + " where [参数名]='" + textbox.Name + "'";
                    m_db.ExecuteSQL(sql);
                }
                else
                {
                    apply_change(c);
                }
            }
        }
        #endregion
        # region 选择工程
        private void buttonX2_Click(object sender, EventArgs e)
        {
            choose_prj frm = new choose_prj(mfrm);
            frm.ShowDialog(this);
            load_para();
        }
        #endregion
        #region 创建单独的参数表
        private void buttonX3_Click(object sender, EventArgs e)
        {
            message_frm frm;
            if (mfrm.curprjid == null || mfrm.curprjid == "")
            {
                frm = new message_frm("没有选择工程", "错误", icon_img.alert1);
                frm.ShowDialog(this);
                frm.Close();
                return;
            }
            frm = new message_frm("是否要为本工程\r\n创建单独的参数表", "询问", icon_img.query);
            frm.ShowDialog(this);
            if (frm.yesorno == true)
            {
                string name = mfrm.curprjid+"_para";
                if(m_db.IsTableInDataBase(name))
                {
                    frm = new message_frm("已存在单独参数表,无法继续新建", "错误", icon_img.alert1);
                    frm.ShowDialog(this);
                    frm.Close();
                    return;
                }
                string sql = "select * into [" + name + "] from [parameter]";
                m_db.ExecuteSQL(sql);
                this.Text = "修改参数---独立参数";
                mfrm.msg("创建单独参数表成功!");
                btn_apply.Enabled = true;
            }
            frm.Close();
        }
        #endregion
        #region 删除独立参数
        private void buttonX4_Click(object sender, EventArgs e)
        {
            message_frm frm;
            if (mfrm.curprjid == null || mfrm.curprjid == "")
            {
                frm = new message_frm("没有选择工程", "错误", icon_img.alert1);
                frm.ShowDialog(this);
                frm.Close();
                return;
            }
            string name = mfrm.curprjid + "_para";
            if (m_db.IsTableInDataBase(name))
            {
                m_db.DeleteTable(name);
                load_para();
                this.Text = "修改参数---公共参数";
                mfrm.msg("已删除独立参数!");
            }
            else
            {
                frm = new message_frm("该工程不存在独立参数!", "错误", icon_img.alert1);
                frm.ShowDialog(this);
                frm.Close();
            }
        }
        #endregion


    }
}
